/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package dreamwar.controller;

import dreamwar.model.MyEntityManagerFactory;
import dreamwar.model.POJO.Feature;
import java.awt.Component;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.RollbackException;
import javax.swing.JOptionPane;

/**
 *
 * @author Berto_Romero
 */
public class FeatureController {
    
    private static FeatureController featureController;
    private final List<Feature> featureList;

    private FeatureController(List<Feature> featureList) {
        this.featureList = featureList;
    }
    
    public static FeatureController getInstance() {
        return featureController;
    }
    
    public static FeatureController getInstance(List<Feature> featureList) {
        if (featureController == null) {
            featureController = new FeatureController(featureList);
        }
        return featureController;
    }
    
    public void createFeature(Component form, String featureName, String featureDescription, int featureCost, boolean isUnitFeature) {
        Feature feature = new Feature(calculateId(featureList));
        feature.setFeatureName(featureName);
        feature.setFeatureDescription(featureDescription);
        feature.setCost(featureCost);
        feature.setUnitFeature(isUnitFeature);
//        EntityManager entityManager = null;
        EntityManager entityManager = MyEntityManagerFactory.getInstance().getEntityManager();
        try {
            entityManager.getTransaction().begin();
            entityManager.persist(feature);
            entityManager.getTransaction().commit();
            featureList.add(feature);
        } catch (RollbackException ex) {
            JOptionPane.showMessageDialog(form, "<html><body><p style='width: 300px;'>" + 
                    "El registro que intenta introducir ya existe.</p></body></html>", 
                    "Calve primaria duplicada", JOptionPane.ERROR_MESSAGE);
        } finally {
            if (entityManager.getTransaction().isActive()) {
                entityManager.getTransaction().rollback();
            }
        }
    }
    
    public void updateFeature(Component form, int idFeature, String featureName) {
        updateFeature(form, idFeature, featureName, "", 0, false);
    }
    
    public void updateFeature(Component form, int idFeature, String featureName, String featureDescription) {
        updateFeature(form, idFeature, featureName, featureDescription, 0, false);
    }
    
    public void updateFeature(Component form, int idFeature, String featureName, int cost) {
        updateFeature(form, idFeature, featureName, "", cost, false);
    }
    
    public void updateFeature(Component form, int idFeature, String featureName, String featureDescription, int cost) {
        updateFeature(form, idFeature, featureName, featureDescription, cost, false);
    }
    
    public void updateFeature(Component form, int idFeature, String featureName, String featureDescription, int cost, boolean isUnitFeature) {
        EntityManager entityManager = null;
//        EntityManager entityManager = MyEntityManagerFactory.getInstance().getEntityManager();
        Feature feature = entityManager.find(Feature.class, idFeature);
        try {
            entityManager.getTransaction().begin();
            feature.setFeatureName(featureName);
            feature.setFeatureDescription(featureDescription);
            feature.setCost(cost);
            feature.setUnitFeature(isUnitFeature);
            entityManager.getTransaction().commit();
        } catch (IllegalArgumentException | RollbackException ex) {
            JOptionPane.showMessageDialog(form, "<html><body><p style='width: 300px;'>" + 
                    "El registro que intenta borrar no existe.</p></body></html>", 
                    "Calve primaria duplicada", JOptionPane.ERROR_MESSAGE);
        } finally {
            if (entityManager.getTransaction().isActive()) {
                entityManager.getTransaction().rollback();
            }
        }
    }
    
    public void deleteFeature(Component form, int idFeature) {
        EntityManager entityManager = null;
//        EntityManager entityManager = MyEntityManagerFactory.getInstance().getEntityManager();
        Feature feature = entityManager.find(Feature.class, idFeature);
        try {
            entityManager.getTransaction().begin();
            entityManager.remove(feature);
            entityManager.getTransaction().commit();
            featureList.remove(feature);
        } catch (IllegalArgumentException | RollbackException ex) {
            JOptionPane.showMessageDialog(form, "<html><body><p style='width: 300px;'>" + 
                    "El registro que intenta borrar no existe.</p></body></html>", 
                    "Calve primaria duplicada", JOptionPane.ERROR_MESSAGE);
        } finally {
            if (entityManager.getTransaction().isActive()) {
                entityManager.getTransaction().rollback();
            }
        }
    }
    
    private int calculateId(List<Feature> featureList) {
        if (featureList.isEmpty()) {
            return 1;
        } else if (featureList.size() == featureList.get(featureList.size() - 1).getIdFeature()) {
            return featureList.size() + 1;
        } else {
            for (int i = 1; i <= featureList.size(); i++) {
                if (featureList.get(i - 1).getIdFeature() > i) {
                    return i;
                }
            }
        }
        return 0;
    }
}
